<template>
	<gui-page ref="guiPage" :customHeader="true">
		<!-- 导航 -->
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('multilingual.print')"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="barcodeSplitting page-auto" :style="'height: calc(100vh - ' + navHeight + 'px);'">
				<!-- 卡片模块 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll element-auto">
					<view style="height: 15rpx"></view>
					<template v-for="(item, index) in data.barcodeInfo" :key="index">
						<view v-if="data.barcodeInfo.length" class="card-boxCustom">
							<view class="card-body">
								<view class="custom-card-title">
									<text class="title-line"></text>
									{{ $t('fgReturn.fgCode') }}:{{item.parentBarcode }}
								</view>
								<view class="custom-card-itemOne">
									<text class="body-item-title">{{ $t('distributedEnter.materialCoded') }}：</text>
									{{ item.barcode }}
								</view>
								<view class="custom-card-itemOne">
									<text class="body-item-title">{{ $t('distributedEnter.materialCode') }}：</text>
									{{ item.materialCode }}
								</view>
								<view class="custom-card-itemOne">
									<text class="body-item-title">{{ $t('distributedEnter.materialName') }}：</text>
									{{ item.materialName }}
								</view>
								<view class="custom-card-itemOne">
									<text class="body-item-title">{{ $t('distributedEnter.materialSpec') }}：</text>{{
										item.materialSpec }}
								</view>
								<view class="custom-card-itemOne">
									<view class="card-item-50">
										<text class="body-item-title">{{ $t('distributedEnter.uom') }}：</text>
										{{ item.unit }}
									</view>
									<view class="card-item-50">
										<text class="body-item-title">{{ $t('distributedEnter.qty') }}：</text>
										{{ item.currentQty }}
									</view>
								</view>
								<view class="custom-card-itemOne">
									<text class="body-item-title">{{ $t('translationImage.warehouseSubordinate') }}：</text>
									{{ item.warehouseCode }}
								</view>
								<view class="custom-card-itemOne">
									<text class="body-item-title">{{ $t('multilingual.locationBelonged') }}：</text>
									{{ item.locationCode }}
								</view>
								<view class="custom-card-itemOne gui-flex gui-justify-content-end">
									<view class="print-class" @click="handelPrint(item)">
										{{ $t('multilingual.printBarcode') }}
									</view>
								</view>
							</view>
						</view>
					</template>

				</scroll-view>
				<!-- 底部确定按钮 -->
				<!-- <view class="page-bottom-box">
					<view class="page-bottomBtn bottomBtn-bgcolor-green" @click="handelPrint">
						{{$t('multilingual.print')}}</view>
				</view> -->
			</view>
		</template>
	</gui-page>
</template>

<script setup>
import { reactive, onMounted, computed } from 'vue'
import pdaScan from '@/hooks/pdaScan.js'
import { printsLabels1 } from '@/api/api.js'
import { BIGBARCODER_PRINT } from '@/constant/index.js'
import { basPdaOperationLogFn } from '@/utils/index.js';
import { OPERATION_PRINT, OPERATION_SCAN } from '@/constant/index.js'
import { TEST_URL, PROD_URL } from "@/constant/config.js"
import { SET_BASEURL } from "@/constant/index.js" // 设置的请求地址
import { PRINT_BASE_URL } from "@/constant/config.js"
import { onShow,onLoad} from '@dcloudio/uni-app';
import {useStore} from "vuex";

//JS国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
//打印机地址
let printUrl = PRINT_BASE_URL

let store = useStore()
let data = reactive({
	isfocus: false,
	barcode: '',
	barcodeInfo: null,
})
const { scan } = pdaScan()
const navHeight = computed(() => {
	return store.state.navHeight
})
onShow(async () => {
	//没有设置默认打印机则去提示设置默认打印机
	const defaultValue = uni.getStorageSync(BIGBARCODER_PRINT) || ''
	if (defaultValue == '') {
		uni.showModal({
			content: t('JsInternational.plsPrinter'),
			showCancel: false,
			confirmText: t('pageTranslation.toSet'),
			success: function (res) {
				if (res.confirm) {
					uni.navigateTo({
						url: '/pages/setting/printer'
					})
				} else if (res.cancel) {

				}
			}
		});
	}
})
onLoad((options)=>{
	// data.barcode=options.barcode
	//data.barcode='M10000737'
})
onMounted(() => {
	// getCode()
})  

data.barcodeInfo = computed(() => {
	return store.state.finishedInventoryPrint
})
// // 获取code
// const getCode = () => {
// 	 var  parme=data.barcode.split(',').filter(item => item != '')
// 	 var  parmes=[]
// 	 parme.forEach(e=>{
// 		 parmes.push({barcode:e})
// 	 })
// 	getProductionRetreatBarcode(parmes).then(res => {
// 		try {
// 			let inputParameter = JSON.stringify(parmes)
// 			let returnParameter = JSON.stringify(res)
// 			let operationPicture = '成品退库退料打印'
// 			let callInterface = res.rquestUrl
// 			let opertionParmes = {
// 				inputParameter,
// 				returnParameter,
// 				operationPicture,
// 				callInterface,
// 				responseCode: res.status,//返回状态
// 				errorMessage: res.message,//错误信息
// 				isPass: res.status == 200 ? 'Y' : 'N',//是否成功 Y N
// 				operationType: OPERATION_SCAN
// 			}
// 			basPdaOperationLogFn(opertionParmes)
// 		} catch (error) {
// 		}
// 		if (res.status == 200&&res.data!=null) {
// 			data.barcodeInfo = res.data
// 		} else {
// 			uni.showToast({ title: res.message, icon: "none" });
// 		}
// 	})
// }

// 打印
const handelPrint = (item) => {
	console.log(item)
	// return
	if (item == null) {
		uni.showToast({ title: t('stockManage.pleaseScanCode'), icon: "none" });
		return
	}
	let defaultValue = uni.getStorageSync(BIGBARCODER_PRINT) || ''
	let parmase = {
		barCode: item.barcode,
		printerName: defaultValue //打印机名字
	}
	// console.log(parmase)
	uni.showLoading({
		title: t('materials.connecting')
	});
	printsLabels1(parmase).then(res => {
		try {
			let inputParameter = JSON.stringify(parmase)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '成品退库退料打印'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code,//返回状态
				errorMessage: res.msg,//错误信息
				isPass: res.code == 0 ? 'Y' : 'N',//是否成功 Y N
				operationType: OPERATION_PRINT,
				module: 'WMS',
				source: "PDA",
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		console.log(res, 'reessss')
		uni.hideLoading();
		if (res.code == 0) {
			uni.showToast({ title: t('JsInternational.printSuccessfully'), icon: "none" });
		} else {
			uni.showToast({ title: res.msg, icon: "none" });
		}
	})
	// .catch(() => {
	// 	try {
	// 		let inputParameter = JSON.stringify(parmase)
	// 		let returnParameter = '无'
	// 		let operationPicture = '成品退库退料打印'
	// 		let callInterface = printUrl + "/print/RawMaterialBarCodePrint"
	// 		let opertionParmes = {
	// 			inputParameter,
	// 			returnParameter,
	// 			operationPicture,
	// 			callInterface,
	// 			responseCode: 408,//返回状态
	// 			errorMessage: '请求超时',//错误信息
	// 			isPass: 'N',//是否成功 Y N
	// 			operationType: OPERATION_PRINT
	// 		}
	// 		basPdaOperationLogFn(opertionParmes)
	// 	} catch (error) {
	// 	}
	// 	uni.showToast({ title: t('materials.printingFailed'), icon: "none" });
	// 	uni.hideLoading();
	// })
//导航栏左侧返回
const clickLeftBack = () => {
	if (!data.scanList.length) {
		uni.navigateBack({
			delta: 1
		});
		return
	}
	uni.showModal({
		title: t('smashRemoval.warmPrompt'),
		content: t('materials.unsubmittedDataIn'),
		success: function (res) {
			if (res.confirm) {
				store.commit("clearFinishedInventoryPrintt")
				uni.navigateBack({
					delta: 1
				});
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}
	//点击物理按键的返回
onBackPress((options) => {
	if (options.from == 'backbutton') {
		if (!data.scanwoodenList.length) {
			uni.navigateBack({
				delta: 1
			});
			return true
		}
		uni.showModal({
			title: t('smashRemoval.warmPrompt'),
			content: t('materials.unsubmittedDataIn'),
			success: function (res) {
				if (res.confirm) {
					//清除store 里面的数据
					store.commit("clearFinishedInventoryPrintt");
					uni.navigateBack({
						delta: 1
					});
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
		return true;
	}
})
}


</script>

<style lang="scss" scoped>
.barcodeSplitting {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
}

@media (prefers-color-scheme: dark) {
	.custom-card-title {
		color: #fff !important;
	}
}


.print-class {
	display: inline-block;
	padding: 10rpx 20rpx;
	background-color: chartreuse;
	color: #fff;
	border-radius: 8rpx;
	background: $uni-color-teal;
}
</style>
